﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JsonTestSite.Controllers
{
    public class JsonTestController : Controller
    {
        //
        // GET: /JsonTest/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult GetIncludeDateTimeField(int year, int month, int day)
        {
            return Json(new { Id = "GetIncludeDateTimeField", Date = new DateTime(year, month, day) }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetDictionary()
        {
            var dict = new Dictionary<string, string>();
            dict.Add("201103180004086698", "tag3");
            dict.Add("201104070004503759", "tag1");
            dict.Add("1098", "tag2");
            return Json(dict, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetKeyValuePairList()
        {
            var dict = new Dictionary<string, string>();
            dict.Add("201103180004086698", "tag3");
            dict.Add("201104070004503759", "tag1");
            dict.Add("1098", "tag2");
            var list = dict.ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetValueTypeArray()
        {
            return Json(new long[] { 2559716940, 2573705502 }, JsonRequestBehavior.AllowGet);
        }
        
        [HttpPost]
        public ActionResult TestFile(int id=1, string desc="", bool sample=false)
        {
            if (!sample)
            {
                var rval =
                    new
                    {
                        id,
                        desc,
                        FileCount = Request.Files.Count,
                        Files = Request
                        .Files
                        .Cast<string>()
                        .Select (f=>Request.Files[f] )
                        .Select(f => new { f.ContentLength, f.ContentType, f.FileName })
                        .ToArray()
                    };
                return Json(rval, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return
                    Json
                    (new
                    {

                        id,
                        desc,
                        FileCount = Request.Files.Count,
                        Files = Enumerable.Range
                        (0, 3).Select(i => new { ContentLength = i, ContentType = i.ToString(), FileName = i + ".exe" }).ToArray()
                    }
                    ,
                    JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet] // http://localhost:14231/jsontest/TestGet?str=hello
        public ActionResult TestGet(string str)
        {
            return
                    Json
                    (new
                    {

                        id=1,
                        desc = str,
                        FileCount = Request.Files.Count,
                        Files = Enumerable.Range
                        (0, 3).Select(i => new { ContentLength = i, ContentType = i.ToString(), FileName = i + ".exe" }).ToArray()
                    }
                    ,
                    JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult TestPost(string str)
        {
            return
                    Json
                    (new
                    {

                        id = 1,
                        desc = str,
                        FileCount = Request.Files.Count,
                        Files = Enumerable.Range
                        (0, 2).Select(i => new { ContentLength = i, ContentType = i.ToString(), FileName = i + ".exe" }).ToArray()
                    }
                    ,
                    JsonRequestBehavior.AllowGet);
        }

    }
}
